﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Moonwish.Silverlight.OsAvatar.Windows
{
    /// <summary>
    /// 标题栏，实现双击事件。
    /// write by zhlin 2012-02-23
    /// </summary>
    public class CaptionBar : Control
    {
        private bool _isClicked;
        /// <summary>
        /// 双击事件。
        /// </summary>
        public event EventHandler DoubleClick;

        public CaptionBar()
        {
            Timeout = 300;
            DefaultStyleKey = typeof(CaptionBar);
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            lock (this)
            {
                if (_isClicked)
                {
                    _isClicked = false;

                    InvokeDoubleClick();
                }
                else
                {
                    base.OnMouseLeftButtonDown(e);
                    _isClicked = true;
                    var start = new ParameterizedThreadStart(ResetThread);
                    new Thread(start).Start();
                }
            }
        }

        private void ResetThread(object state)
        {
            Thread.Sleep(Timeout);
            lock (this)
            {
                if (_isClicked)
                {
                    _isClicked = false;
                }
            }
        }

        /// <summary>
        /// 调用双击事件。
        /// </summary>
        void InvokeDoubleClick()
        {
            if (DoubleClick != null)
            {
                DoubleClick(this, new EventArgs());
            }
        }

        /// <summary>
        /// 双击事件距。
        /// </summary>
        public int Timeout { get; set; }
        /// <summary>
        /// 图标
        /// </summary>
        public ImageSource Favicon
        {
            get { return (ImageSource)GetValue(FaviconProperty); }
            set { SetValue(FaviconProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty FaviconProperty = DependencyProperty.Register("Favicon",
                                                                                                typeof(ImageSource),
                                                                                                typeof (CaptionBar),
                                                                                                new PropertyMetadata(
                                                                                                    new BitmapImage(new Uri("/Moonwish.Silverlight.OsAvatar;component/Icons/ie.png", UriKind.Relative))));
        /// <summary>
        /// 标题
        /// </summary>
        public string Caption
        {
            get { return (string)GetValue(CaptionProperty); }
            set { SetValue(CaptionProperty, value); }
        }
        /// <summary>
        /// 标题
        /// </summary>
        public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("Caption",
                                                                                                typeof(string),
                                                                                                typeof(CaptionBar),
                                                                                                new PropertyMetadata(
                                                                                                    string.Empty));
    }
}
